﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using DBUtility;
using IDAL;
using Model;

namespace DAL
{
    public class TSemiProduct : BaseRepository<TSemiProductInfo>, ITSemiProduct 
    {
        private const string PARM_TSEMIPRODUCT_SEMIBATCHID = "@SemiBatchID";
        private const string PARM_TSEMIPRODUCT_RECORDTIME = "@RecordTime";
        private const string PARM_TSEMIPRODUCT_USERID = "@UserID";

        private const string PARM_ASSEMBLEDETAILITEM_DT = "@TAssembleDetailItemdt";

        public bool AssembleDetailItems_Insert(TSemiProductInfo ad, DataTable itemdt)
        {
            List<SqlParameter> ItemList = new List<SqlParameter>() {
            sqlHelper.CreateSqlParemeterStructured(PARM_ASSEMBLEDETAILITEM_DT,itemdt)};
            SqlParameter[] Parms = GetParameters();
            SetParameters(ad, Parms);

            SqlCommand cmd = new SqlCommand();
            foreach (SqlParameter parm in Parms)
                cmd.Parameters.Add(parm);
            foreach (SqlParameter parm in ItemList)
                cmd.Parameters.Add(parm);

            using (SqlConnection conn = new SqlConnection(sqlHelper.ConnectionStringLocalTransaction))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                try
                {
                    cmd.CommandText = "TSemiProduct_Insert";
                    using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { }
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        public IList<TSemiProductInfo> LoadEntities()
        {
            return null;
        }

        /// <summary>
        /// Get an individual category based on a provided id
        /// </summary>
        /// <param name="categoryId">Category id</param>
        /// <returns>Details about the Category</returns>
        public TSemiProductInfo LoadEntity<T1>(T1 semiBatchID)
        {
            SqlParameter parm = GetParameter();
            parm.Value = semiBatchID;
            return ExcuteSqlToGetEntity("TSemiProduct_selectLastbySN", parm);
        }

        public IList<TSemiProductInfo> GetAssembleDetailbySN(string semiBatchID)
        {
            SqlParameter parm = GetParameter();
            parm.Value = semiBatchID;
            return ExcuteSqlToGetEntities("TSemiProduct_selectbySN", parm);
        }




        //-------------------------------------重写超类基础操作---------------------------------------------------------

        public override TSemiProductInfo GetEntityRecord(SqlDataReader rdr)
        {
            TSemiProductInfo pro = new TSemiProductInfo(rdr.GetValue(0).ToString(), rdr.GetDateTime(1), rdr.GetValue(2).ToString());
            return pro;
        }

        public override TSemiProductInfo GetEntityRecord()
        {
            TSemiProductInfo pro = new TSemiProductInfo();
            return pro;
        }

        public override string GetInsertsp()
        {
            return null;
        }

        public override string GetUpdatesp()
        {
            return null;
        }

        public override string GetDeletesp()
        {
            return null;
        }

        public override SqlParameter GetParameter()
        {
            SqlParameter Parm = new SqlParameter(PARM_TSEMIPRODUCT_SEMIBATCHID, SqlDbType.NVarChar, 50);
            return Parm;
        }

        public override SqlParameter[] GetParameters()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
					new SqlParameter(PARM_TSEMIPRODUCT_SEMIBATCHID, SqlDbType.NVarChar, 50),
                    new SqlParameter(PARM_TSEMIPRODUCT_RECORDTIME,SqlDbType.DateTime,8),
					new SqlParameter(PARM_TSEMIPRODUCT_USERID, SqlDbType.NVarChar, 10)
                };
            return parms;
        }

        public override void SetParameters(TSemiProductInfo ad, params SqlParameter[] Parms)
        {
            Parms[0].Value = ad.SemiBatchID;
            Parms[1].Value = ad.RecordTime;
            Parms[2].Value = ad.UserID;
        }
    }
}
